package com.example.rentingapp.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.example.rentingapp.R;
import com.example.rentingapp.adapter.ParentCategoryAdapter;

public class CommonFilterPop extends PopupWindow {
    private LayoutInflater mInflater;                       //布局填充器
    private Activity mActivity;                             //上下文
    private View popupView;                                 //pop整体View
    private String[] mData;                                 //只显示String类型的数据
    private ListView lvContent;                             //选择条件的list
    private Button btn_common_pop;                          //按钮
    private ParentCategoryAdapter parentCategoryAdapter;    //适配器
    private SelectCategory selectCategory = null;           //监听器
    private DisplayMetrics dm;                              //获取手机屏幕分辨率的类

    //构造函数
    public CommonFilterPop(Activity activity, String[] mData, SelectCategory selectCategory) {
        this.selectCategory = selectCategory;
        this.mData = (mData);
        this.mActivity = activity;
        this.mInflater = LayoutInflater.from(mActivity);
        popupView = mInflater.inflate(R.layout.common_popup_list_dialog, null);             //布局填充
        this.setContentView(popupView);                                                     //设置View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);                                 //设置弹出窗体的宽
        dm = new DisplayMetrics();                                                          //获取屏幕高度
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        this.setHeight((dm.heightPixels)*1/3);                                              //设置弹出窗体的高
        initPopView();                                                                      //初始化控件
        this.setFocusable(true);                                                            //设置焦点
        this.setTouchable(true);                                                            //内部点击事件
        this.setOutsideTouchable(true);                                                     //外部点击事件
        this.setBackgroundDrawable(new BitmapDrawable());                                   //设置背景
        this.setAnimationStyle(R.style.pop_anim);                                           //设置加载动画
        this.update();
    }

    public void initPopView() {
        lvContent = (ListView) popupView.findViewById(R.id.lv_item);
        btn_common_pop=(Button)popupView.findViewById(R.id.btn_common_pop);
        parentCategoryAdapter = new ParentCategoryAdapter(mActivity, mData);
        lvContent.setAdapter(parentCategoryAdapter);

        lvContent.setOnItemClickListener(parentItemClickListener);
        btn_common_pop.setOnClickListener(btnItemClickListener);
    }

    //ListView点击事件
    private OnItemClickListener parentItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            parentCategoryAdapter.setSelectedPosition(position);
            parentCategoryAdapter.notifyDataSetChanged();

            selectCategory.selectCategory(parentCategoryAdapter.getPos());
            dismiss();
        }
    };

    //Button点击事件
    private OnClickListener btnItemClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    };

    //选择成功回调
    public interface SelectCategory {
        //parentSelectposition   父类别选中下标
        void selectCategory(int parentSelectPosition);
    }
}
